package org.linlinjava.litemall.db.service;

import com.github.yulichang.base.MPJBaseServiceImpl;
import org.linlinjava.litemall.db.dao.LitemallRewardRuleMapper;
import org.linlinjava.litemall.db.domain.LitemallRewardRule;
import org.linlinjava.litemall.db.domain.LitemallRewardRuleExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * @program: litemall
 * @description: litemall_reward_rule
 * @author: chenyu
 * @create: 2022-03-15 10:59
 **/
@Service
public class LitemallRewardRuleService extends MPJBaseServiceImpl<LitemallRewardRuleMapper,LitemallRewardRule> {
  @Resource
  private LitemallRewardRuleMapper litemallRewardRuleMapper;

  public void add(LitemallRewardRule rule){
    litemallRewardRuleMapper.insertSelective(rule);
  }

  public void delete(Integer ruleId){
    litemallRewardRuleMapper.logicalDeleteByPrimaryKey(ruleId);
  }

  public void update(LitemallRewardRule rule){
    litemallRewardRuleMapper.updateByPrimaryKeySelective(rule);
  }

  public List<LitemallRewardRule> list(String ruleName,Byte status,List<Byte> methods,boolean isExpired){

    LitemallRewardRuleExample example = new LitemallRewardRuleExample();
    LitemallRewardRuleExample.Criteria criteria = example.createCriteria();

    if(status != null){
      criteria.andStatusEqualTo(status);
    }

    if(methods != null){
      criteria.andGiveMethodIn(methods);
    }

    if(isExpired){
      criteria.andEndDateGreaterThan(LocalDate.now());
    }


    if(ruleName != null){
      criteria.andNameLike("%"+ ruleName +"%");
    }

    criteria.andDeletedEqualTo(false);

    return litemallRewardRuleMapper.selectByExample(example);
  }

  public List<LitemallRewardRule> listNot(String ruleName,Byte status,List<Byte> methods){

    LitemallRewardRuleExample example = new LitemallRewardRuleExample();
    LitemallRewardRuleExample.Criteria criteria = example.createCriteria();
    criteria.andDeletedEqualTo(false);

    if(status != null){
      criteria.andStatusEqualTo(status);
    }

    if(methods != null){
      criteria.andGiveMethodIn(methods);
    }

    if(ruleName != null){
      criteria.andNameLike("%"+ ruleName +"%");
    }
    criteria.andDeletedEqualTo(false);

    return litemallRewardRuleMapper.selectByExample(example);
  }

  public List<LitemallRewardRule> listNewAndNormal(String ruleName,List<Byte> status){

    LitemallRewardRuleExample example = new LitemallRewardRuleExample();
    LitemallRewardRuleExample.Criteria criteria = example.createCriteria();
    criteria.andDeletedEqualTo(false);

    if(status != null){
      criteria.andStatusIn(status);
    }

    criteria.andEndDateLessThan(LocalDate.now());

    if(ruleName != null){
      criteria.andNameLike("%"+ ruleName +"%");
    }

    return litemallRewardRuleMapper.selectByExample(example);
  }

  public LitemallRewardRule findById(Integer ruleId){
    return litemallRewardRuleMapper.selectByPrimaryKey(ruleId);
  }

  public List<LitemallRewardRule> findByDate(LocalDate startDate,LocalDate endDate){
    LitemallRewardRuleExample example = new LitemallRewardRuleExample();
    LitemallRewardRuleExample.Criteria criteria = example.createCriteria();
    criteria.andDeletedEqualTo(false);



    return litemallRewardRuleMapper.selectByExample(example);
  }

  public int updateBatchById(Byte status,List<Integer> ids){
    return litemallRewardRuleMapper.updateBatchById(status,ids);
  }
}
